/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.dao;

import br.com.modelo.AptoBean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Felipe
 */
public class AptoDao {
    private Connection connection;
    
   public AptoDao(){
       this.connection =new ConnectionFactory().getConnection();
   }

   public void adiciona(AptoBean ab){
       String sql = "insert into apto"+
               "(TIPO_APTO_CD_TIPO_APTO,TP_SITUACAO)values(?,?)";
       
       
       try{
           PreparedStatement stmt = connection.prepareStatement(sql);
           stmt.setInt(1, ab.getCd_tipo_apto());          
           stmt.setString(1,ab.getTipo_situacao());
           stmt.execute();
           stmt.close();
       }catch(SQLException e){
           throw new RuntimeException(e);
       }
   }
       
       public List<AptoBean> getLista() {
		try {
			//cria um list do tipo AptoBean
			List<AptoBean> listadeaptos = new ArrayList<AptoBean>();
			//cria objeto para injetar comando tipo select no banco
			PreparedStatement stmt = this.connection
					.prepareStatement("select * from APTO");
			//objeto resultset consegue guardar valor encontrado
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				// Criando o objeto apartamento
				AptoBean ab = new AptoBean();
				//Seta no objeto apartamento o que o "rs" pegou no banco
				ab.setCod_apto(rs.getInt("CD_APTO"));
				ab.setTipo_situacao(rs.getString("TP_SITUACAO"));
				ab.setCd_tipo_apto(rs.getInt("TIPO_APTO_CD_TIPO_APTO"));
				
				// adicionando apartamento para a lista
				listadeaptos.add(ab);
			}
			rs.close();
			stmt.close();
			return listadeaptos;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public void altera(AptoBean   apto) {

		String sql = "update apto set TP_SITUACAO=? where CD_APTO=?";
		PreparedStatement stmt;
		try {
			stmt = connection.prepareStatement(sql);

			stmt.setString(1,apto.getTipo_situacao());
			
			stmt.execute();
			stmt.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public void remove(AptoBean apto) {
		PreparedStatement stmt;
		try {
			stmt = connection.prepareStatement("delete from APTO where CD_APTO=?");
			stmt.setInt(1, apto.getCod_apto());
			stmt.execute();
			stmt.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

}

       
       
       
       
       
   
   
   
   
